package com.huiyujinrong.qiangdanbao.activity.fragment;


import android.content.Context;

import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.DaggerModelComponent;
import com.huiyujinrong.qiangdanbao.bean.BannerBean;
import com.huiyujinrong.qiangdanbao.bean.FragmentGrabSingleBean;
import com.huiyujinrong.qiangdanbao.bean.GrabsBean;
import com.huiyujinrong.qiangdanbao.model.GrabSingleModel;
import com.huiyujinrong.qiangdanbao.network.Api;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import happyloan.core.RespEvent;

public class FragmentGrabSinglePresenter implements FragmentGrabSingleContract.Presenter {


    private FragmentGrabSingleContract.View view;
    private Context context;

    @Inject
    GrabSingleModel model;

    public FragmentGrabSinglePresenter(FragmentGrabSingleContract.View view, Context context) {
        this.view = view;
        this.context = context;
        setupDagger2();
    }

    private void setupDagger2() {
        DaggerModelComponent.builder().appComponent(BorrowHelpApp.get().getAppComponent()).build().inject(this);
    }

    @Override
    public void getGrabSingleData(String city_code,
                                  String loan_type,
                                  int page_no,
                                  int page_size,
                                  String user_type,
                                  String loan_use,
                                  String apply_amt,
                                  String loan_term,
                                  String social_security_state,
                                  String house_fund_state,
                                  String industry_type,
                                  String license_years,
                                  String bank_dgls,
                                  String bank_dsls) {
        model.getGrabSingle(city_code,
                loan_type,
                page_no,
                page_size,
                user_type,
                loan_use,
                apply_amt,
                loan_term,
                social_security_state,
                house_fund_state,
                industry_type,
                license_years,
                bank_dgls,
                bank_dsls);
    }

    @Override
    public void getBannerHolder() {
        model.getBannerHolder();
    }

    @Override
    public void start() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void end() {
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getGrabSingleData(RespEvent<FragmentGrabSingleBean> event) {
        if (event.getUrl().equals(Api.getGrabSingleData)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
//                    FragmentGrabSingleBean ben = event.getT();
//                    Gson gson = new Gson();
//                    String d = gson.toJson(ben);
//                    Log.i("result", "getGrabSingleData: " + d);
                    view.setViewDta(event.getT());
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if (event.isAlert()) {
                        view.showToast(String.valueOf(event.getExtra()));
                        view.noData();
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好，稍后重试");
                    view.noData();
                    break;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void respEventBannerHolder(RespEvent<BannerBean> event) {
        if (event.getUrl().equals(Api.getBannerHolder)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.setBannerHolder(event.getT());
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if (event.isAlert()) {
                        view.showToast(String.valueOf(event.getExtra()));
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好,稍后再试");
                    break;
            }
        }
    }

    public void findRealTimeGrab() {
        model.findRealTimeGrab();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void findRealTimeGrab(RespEvent<GrabsBean> event) {
        if (event.getUrl().contains(Api.findRealTimeGrab)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.findRealTimeGrab(event.getT());
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if (event.isAlert()) {
                        view.showToast(String.valueOf(event.getExtra()));
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好,稍后再试");
                    break;
            }
        }
    }
}
